package com.hotel.controller;

import com.hotel.bean.RespBean;
import com.hotel.service.OrderService;
import com.hotel.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/admin/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private RoomService roomService;

    //查询所有
    @GetMapping
    public RespBean selectAll(){
        return RespBean.ok("",orderService.selectAll());
    }
    //获取房间信息
    @GetMapping("/allRoom/{oid}")
    public RespBean allRoom(@PathVariable("oid") Integer oid){
        List<Map<String,Object>> rooms = roomService.selectRoomAndOrderRoom(oid)
                .stream()
                .filter(e -> e.getRstate() != 3)
                .filter(e -> e.getRstate() != 2)
                .map(e -> {
                    Map<String, Object> roomMap = new HashMap<>();
                    roomMap.put("key",e.getRid());
                    roomMap.put("label",e.getRnumber() + "-" +e.getRname());
                    return roomMap;
                }).collect(Collectors.toList());
        //获取已经预定的房间
        List<Integer> rids = orderService.selectRidByOid(oid);
        Map<String ,Object> map = new HashMap<>();
        map.put("allRoomList",rooms);
        map.put("selectedRids",rids);
        return RespBean.ok("",map);
    }
    //添加
    @PostMapping("/oidAndRid")
    public RespBean insertOidAndRid(@RequestBody Map<String,Object>map){
        Integer oid = (Integer) map.get("oid");
        List<Integer> ridsList = (List<Integer>) map.get("rids");
        Integer[] rids = new Integer[ridsList.size()];
        for (int i = 0; i < ridsList.size(); i++) {
            int rid = ridsList.get(i);
            rids[i] = new Integer(rid+"");
        }

        orderService.insertOidAndRid(oid,rids);
        return RespBean.ok("预定房间成功");
    }
    @PutMapping("/{oid}")
    public RespBean updateOstate(@PathVariable("oid") Integer oid) {
        orderService.updateOstate(oid);
        return RespBean.ok("已完成订单");
    }

}
